﻿using System;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.Pages;
using Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.ViewModels;

namespace Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.ViewFacades
{
	public class WelcomeViewFacade : AbstractLocationSenderAppViewFacade<WelcomePage, WelcomePageViewModel>, IWelcomeView
	{
		#region Events
		public event Action DoSelectContacts = delegate { };
		public event Action DoResend = delegate { }; 
		public event Action DoOpenAboutView = delegate { };
		public event Action DoLaunchAppRateLink = delegate { };
		public event Action DoOpenSettingsView = delegate { }; 
		#endregion

		#region Instance Variables (EMPTY)
		#endregion

		#region Properties
		public bool CanResend
		{
			get { return myViewModel.CanResend; }
			set
			{
				if (DoNotAccessViewModel ()) return;
				InvokeSet (val => {
				           	if (DoNotAccessViewModel ()) return;
				           	myViewModel.HasBeenLoaded = true;
				           	myViewModel.CanResend = val;
				           },
				           value);
			}
		}
		#endregion

		#region Constructors
		public WelcomeViewFacade ()
		{
			Init (false);
		}

		public WelcomeViewFacade (WelcomePage page)
		{
			myPage = page;
			Init (true);
		}

		private void Init (bool wireUp)
		{
			myViewModel = new WelcomePageViewModel ();
			myViewModel.IsAssociatedWithViewFacadeAndPage = true;
			myViewModel.HasBeenLoaded = false;
			if (wireUp) WireUpPage ();
		}
		#endregion

		#region Implementation of IView/IWelcomeView
		// no methods to implement
		#endregion

		/// <summary>
		/// Unsubscribes view event handlers.
		/// Call base impl as last step.
		/// </summary>
		protected override void UnWirePage ()
		{
			if (myPage == null) return;

			// IWelcomeView
			myPage.DoSelectContacts -= FireDoSelectContacts;
			myPage.DoResend -= FireDoResend;
			myPage.DoOpenAboutView -= FireDoOpenAboutView;
			myPage.DoLaunchAppRateLink -= FireDoLaunchAppRateLink;
			myPage.DoOpenSettingsView -= FireDoOpenSettingsView;

			base.UnWirePage ();
		}

		/// <summary>
		/// Subscribes view event handlers.
		/// Call base impl as first step.
		/// </summary>
		protected override void WireUpPage ()
		{
			if (myPage == null) return;

			base.WireUpPage ();

			// IWelcomeView
			myPage.DoSelectContacts += FireDoSelectContacts;
			myPage.DoResend += FireDoResend;
			myPage.DoOpenAboutView += FireDoOpenAboutView;
			myPage.DoLaunchAppRateLink += FireDoLaunchAppRateLink;
			myPage.DoOpenSettingsView += FireDoOpenSettingsView;
		}

		protected void FireDoSelectContacts ()
		{ DoSelectContacts (); }
		protected void FireDoResend ()
		{ DoResend (); }
		protected void FireDoOpenAboutView ()
		{ DoOpenAboutView (); }
		protected void FireDoLaunchAppRateLink ()
		{ DoLaunchAppRateLink (); }
		protected void FireDoOpenSettingsView()
		{ DoOpenSettingsView (); }
	}
}
